{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make separate files\n",
    "\n",
    "My predictive modeling software is designed to run with separate tsv files for each document. The reasons for this are purely legacy-code, but I don't have time to fix it just at the moment. So let's create some separate files!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import csv\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "501\n",
      "1001\n",
      "1501\n",
      "2001\n",
      "2501\n",
      "3001\n",
      "3501\n",
      "4001\n",
      "4501\n",
      "5001\n",
      "5501\n",
      "6001\n",
      "6501\n"
     ]
    }
   ],
   "source": [
    "docfreqs = pd.read_csv('docfrequencies.tsv', sep = '\\t')\n",
    "features = set(docfreqs.word)\n",
    "\n",
    "def write_file(docid, featurecounts, totalcount):\n",
    "    root = '../data/'\n",
    "    filepath = root + docid + '.tsv'\n",
    "    with open(filepath, mode = 'w', encoding = 'utf-8') as f:\n",
    "        f.write('feature\\tfrequency\\n')\n",
    "        for word, count in featurecounts.items():\n",
    "            f.write(word + '\\t' + str(count/totalcount) + '\\n')\n",
    "\n",
    "with open('../parsejsons/rawcounts.tsv', encoding = 'utf-8') as f:\n",
    "    header = False\n",
    "    ctr = 0\n",
    "    \n",
    "    for line in f:\n",
    "        if not header:\n",
    "            header = True\n",
    "            wordcounts = Counter()\n",
    "            lastdoc = ''\n",
    "            totalcount = 0\n",
    "            \n",
    "        else:\n",
    "            row = line.strip().split('\\t')\n",
    "            docid = row[0]\n",
    "            word = row[1]\n",
    "            count = int(row[2])\n",
    "            if lastdoc == '':\n",
    "                lastdoc = docid\n",
    "            \n",
    "            if lastdoc!= docid:\n",
    "                write_file(lastdoc, wordcounts, totalcount)\n",
    "                wordcounts = Counter()\n",
    "                totalcount = 0\n",
    "                lastdoc = docid\n",
    "                ctr += 1\n",
    "                if ctr % 500 == 1:\n",
    "                    print(ctr)\n",
    "            \n",
    "            totalcount += count\n",
    "            if word in features:\n",
    "                wordcounts[word] += count\n",
    "    \n",
    "    write_file(lastdoc, wordcounts, totalcount)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
